package com.pda.yuanjing.pages.login;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatButton;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.pda.yuanjing.PDApp;
import com.pda.yuanjing.R;
import com.pda.yuanjing.beanout.CidInfoBeanOut;
import com.pda.yuanjing.beanout.DealerBeanOut;
import com.pda.yuanjing.beanout.LoginBeanOut;
import com.pda.yuanjing.beanout.LogoBeanOut;
import com.pda.yuanjing.beanout.ProductBeanOut;
import com.pda.yuanjing.pages.config.ConfigActivity;
import com.pda.yuanjing.pages.main.MainActivity;
import com.pda.yuanjing.tools.StringUtils;
import com.public_module.basemvp.activity.BaseActivity;
import com.public_module.tools.CheckVersion;
import com.public_module.tools.SharedPreferencesManger;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity<LoginModel, LoginView, LoginPresent> implements LoginView {
    private String tag = "LoginActivity";

    @BindView(R.id.tvname)
    TextView tvname;
    @BindView(R.id.cb)
    CheckBox cb;
    @BindView(R.id.ivlogo)
    ImageView ivlogo;
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rltitle)
    RelativeLayout rltitle;
    @BindView(R.id.login_phone)
    EditText loginPhone;
    @BindView(R.id.login_pass)
    EditText loginPass;
    @BindView(R.id.btlogin)
    AppCompatButton btlogin;
    @BindView(R.id.btconfig)
    AppCompatButton btconfig;
    @BindView(R.id.tvforpass)
    TextView tvforpass;
    @BindView(R.id.tversion)
    TextView tversion;

    boolean flag = false;
    String  user, pw, sn = "web";
    Gson gson = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        tvTitle.setText("登录");
        tversion.setText("版本号:V" + CheckVersion.getVersionName(this));
        flag = SharedPreferencesManger.getRem();
        cb.setChecked(flag);
        if (flag) {
            loginPhone.setText(SharedPreferencesManger.getUname());
            loginPass.setText(SharedPreferencesManger.getUpass());
        }
        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                flag = isChecked;
            }
        });
        gson = new Gson();
//        loginPhone.setText("test11");
//        loginPass.setText("123456");
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (SharedPreferencesManger.getLoacl()) {
            loginPhone.setVisibility(View.INVISIBLE);
            loginPass.setVisibility(View.INVISIBLE);
            cb.setVisibility(View.INVISIBLE);
            try {
                String jsonConfig = StringUtils.readTxtToFile("/PDADataImport/logo.txt");
                LogoBeanOut  logoBeanOut= gson.fromJson(jsonConfig, LogoBeanOut.class);
                Glide.with(mContext).load(logoBeanOut.getData().getLogo()).into(ivlogo);
                tvname.setText(logoBeanOut.getData().getName());
            }catch (Exception e){
                e.printStackTrace();
            }
        } else if(SharedPreferencesManger.getCid().length()>0){
            loginPhone.setVisibility(View.VISIBLE);
            loginPass.setVisibility(View.VISIBLE);
            cb.setVisibility(View.VISIBLE);
            presenter.logo(SharedPreferencesManger.getHostName());
        }
    }

    @Override
    public LoginModel createModel() {
        return new LoginModelImpl(this);
    }

    @Override
    public LoginView createView() {
        return this;
    }

    @Override
    public LoginPresent createPresenter() {
        return new LoginPresent();
    }

    @Override
    public void loginSucess(List<LoginBeanOut.DataBean.ShowInfoBean> list) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                SharedPreferencesManger.setRem(flag);
                intentToActivityWithSerializableParameter(LoginActivity.this, MainActivity.class, PDApp.showListKey, (Serializable) list);
                finish();
            }
        });
    }

    @Override
    public void loginLogo(LogoBeanOut.DataBean dataBean) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Glide.with(mContext).load(dataBean.getLogo()).into(ivlogo);
                tvname.setText(dataBean.getName());
            }
        });
    }

    @OnClick({R.id.iv_title_left, R.id.btlogin, R.id.btconfig})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.btconfig:
                intentToActivityWithoutParameter(this, ConfigActivity.class);
                break;
            case R.id.btlogin:
                if (SharedPreferencesManger.getLoacl()) {
                    try {
                        String jsonLogin = StringUtils.readTxtToFile("/PDADataImport/login.txt");
                        LoginBeanOut loginBeanOut = gson.fromJson(jsonLogin, LoginBeanOut.class);
                        SharedPreferencesManger.setUid(loginBeanOut.getData().getUserID());
                        SharedPreferencesManger.setStoreID(loginBeanOut.getData().getStoreID());
                        SharedPreferencesManger.setStoreCode(loginBeanOut.getData().getStoreCode());
                        SharedPreferencesManger.setStoreName(loginBeanOut.getData().getStoreName());
                        String jsonConfig = StringUtils.readTxtToFile("/PDADataImport/config.txt");
                        PDApp.cidInfoBeanOut = gson.fromJson(jsonConfig, CidInfoBeanOut.class);
                        String jsonDealer = StringUtils.readTxtToFile("/PDADataImport/dealer.txt");
                        PDApp.dealerBeanOut = gson.fromJson(jsonDealer, DealerBeanOut.class);
                        String jsonProduct = StringUtils.readTxtToFile("/PDADataImport/product.txt");
                        PDApp.productBeanOut = gson.fromJson(jsonProduct, ProductBeanOut.class);
                        if (loginBeanOut == null || PDApp.cidInfoBeanOut == null || PDApp.dealerBeanOut == null || PDApp.productBeanOut == null) {
                            showMsg("基础信息文件缺失");
                            return;
                        }
                        intentToActivityWithSerializableParameter(LoginActivity.this, MainActivity.class, PDApp.showListKey, (Serializable) loginBeanOut.getData().getShowInfo());
                    } catch (Exception e) {
                        showMsg("基础信息文件缺失");
                        e.printStackTrace();
                    } finally {
                        return;
                    }
                }
                if (SharedPreferencesManger.getCid().length() == 0) {
                    showMsg("请设置企业编号");
                    return;
                }
                user = loginPhone.getText().toString();
                pw = loginPass.getText().toString();
                if (user.length() == 0) {
                    showMsg("请填写用户名");
                    return;
                }
                if (pw.length() == 0) {
                    showMsg("请填写密码");
                    return;
                }
                presenter.login(SharedPreferencesManger.getHostName(), user, pw, sn);
                break;
        }
    }
}
